#include "MutexLock.h"
#include "Condition.h"
#include <stdio.h>
    
Condition::Condition(MutexLock &mutex)
: _mutex(mutex){
    int ret = pthread_cond_init(&_cond, nullptr);
    if(ret){
        perror("pthread_cond_init");
        return;
    }
}


Condition::~Condition(){
    int ret = pthread_cond_destroy(&_cond);
    if(ret){
        perror("pthread_cond_destroy");
        return;
    }
}


void Condition::wait(){
    int ret = pthread_cond_wait(&_cond, _mutex.getMutexLockPtr());
    if(ret){
        perror("pthread_cond_wait");
        return;
    }
}


// 唤醒单个线程
void Condition::notify(){
    int ret = pthread_cond_signal(&_cond);
    if(ret){
        perror("pthread_cond_signal");
        return;
    }
}


// 广播唤醒所有线程
void Condition::notifyAll(){
    int ret = pthread_cond_broadcast(&_cond);
    if(ret){
        perror("pthread_cond_broadcast");
        return;
    }
}
